package com.janwen.spring.aop.profile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.util.StopWatch;

public class SimpleProfiler implements Ordered {

	
	final static Log logger = LogFactory.getLog(SimpleProfiler.class);
	private int order;
	public void setOrder(int order) {
		this.order = order;
	}
	@Override
	public int getOrder() {
		return this.order;
	}
	
	
	
	// this method is the around advice
	  public Object profile(ProceedingJoinPoint call) throws Throwable {
	    Object returnValue;
	    StopWatch clock = new StopWatch(getClass().getName());
	    try {
	      clock.start(call.toShortString());
	      returnValue = call.proceed();
	    } finally {
	      clock.stop();
	     logger.info(clock.prettyPrint());
	    }
	    return returnValue;
	  }

}
